package com.chaihu.api.listener.admin.menu;

import com.alibaba.fastjson.JSONObject;
import com.chaihu.api.listener.AbstractServiceApiListener;
import com.chaihu.bean.in.admin.menu.UpdateMenuIn;
import com.chaihu.bean.result.ResponseResult;
import com.chaihu.core.annotation.ChaihuListener;
import com.chaihu.core.context.DataFlowContext;
import com.chaihu.core.event.service.api.ServiceDataFlowEvent;
import com.chaihu.intf.admin.menu.UpdateMenuService;
import com.chaihu.utils.constant.ServiceCodeMenuConstant;
import com.chaihu.utils.util.Assert;
import com.chaihu.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@ChaihuListener("updateMenuListener")
public class UpdateMenuListener extends AbstractServiceApiListener {

    @Autowired
    private UpdateMenuService updateMenuService;

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "mId", "菜单ID不能为空");
        Assert.hasKeyAndValue(reqJson, "name", "必填，请填写菜单名称");
        Assert.hasKeyAndValue(reqJson, "url", "必填，请菜单菜单地址");
        Assert.hasKeyAndValue(reqJson, "seq", "必填，请填写序列");
        Assert.hasKeyAndValue(reqJson, "isShow", "必填，请选择是否显示菜单");

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        UpdateMenuIn menuGroupDto = BeanConvertUtil.covertBean(reqJson, UpdateMenuIn.class);
        ResponseResult<Void> responseResult = updateMenuService.updateMenu(menuGroupDto);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(responseResult), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    @Override
    public String getServiceCode() {
        return ServiceCodeMenuConstant.UPDATE_MENU;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }

    @Override
    public int getOrder() {
        return DEFAULT_ORDER;
    }

}
